package com.flyco.modules.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.pms.entity.PmsComment;
import com.flyco.modules.pms.entity.PmsCommentReplay;
import com.flyco.modules.pms.service.IPmsCommentReplayService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.pms.service.IPmsCommentService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 商品评价回复
 * @Author: flyco
 * @Date:   2020-02-27
 * @Version: V1.0
 */
@Slf4j
@Api(tags="商品评价回复")
@RestController
@RequestMapping("/f/pms/pmsCommentReplay")
public class PmsCommentReplayController extends BaseController<PmsCommentReplay, IPmsCommentReplayService> {
	 @Autowired
	 private IPmsCommentReplayService pmsCommentReplayService;
	 @Autowired
	 private IPmsCommentService pmsCommentService;
	
	/**
	 * 分页列表查询
	 *
	 * @param pmsCommentReplay
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商品评价回复-分页列表查询")
	@ApiOperation(value="商品评价回复-分页列表查询", notes="商品评价回复-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PmsCommentReplay pmsCommentReplay,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PmsCommentReplay> queryWrapper = QueryGenerator.initQueryWrapper(pmsCommentReplay, req.getParameterMap());
		Page<PmsCommentReplay> page = new Page<PmsCommentReplay>(pageNo, pageSize);
		IPage<PmsCommentReplay> pageList = pmsCommentReplayService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param pmsCommentReplay
	 * @return
	 */
	@AutoLog(value = "商品评价回复-添加")
	@ApiOperation(value="商品评价回复-添加", notes="商品评价回复-添加")
	@PostMapping(value = "/add")
	@Transactional
	public Result<?> add(@RequestBody PmsCommentReplay pmsCommentReplay) {
		PmsComment updateComment = new PmsComment();
		PmsComment pmsComment = pmsCommentService.getById(pmsCommentReplay.getCommentId());
		if (oConvertUtils.isEmpty(pmsComment.getReplayCount())){
			updateComment.setReplayCount(1);
		} else {
			updateComment.setReplayCount(pmsComment.getReplayCount()+1);
		}
		updateComment.setId(pmsComment.getId());
		pmsCommentService.updateById(updateComment);

		pmsCommentReplay.setMemberNickName("飞科客服");
		pmsCommentReplay.setMemberIcon("https://pic.flyco.net.cn/mall/newfeike.jpg");
		pmsCommentReplay.setType("1");
		pmsCommentReplayService.save(pmsCommentReplay);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param pmsCommentReplay
	 * @return
	 */
	@AutoLog(value = "商品评价回复-编辑")
	@ApiOperation(value="商品评价回复-编辑", notes="商品评价回复-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PmsCommentReplay pmsCommentReplay) {
		pmsCommentReplayService.updateById(pmsCommentReplay);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品评价回复-通过id删除")
	@ApiOperation(value="商品评价回复-通过id删除", notes="商品评价回复-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		pmsCommentReplayService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商品评价回复-批量删除")
	@ApiOperation(value="商品评价回复-批量删除", notes="商品评价回复-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.pmsCommentReplayService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品评价回复-通过id查询")
	@ApiOperation(value="商品评价回复-通过id查询", notes="商品评价回复-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		PmsCommentReplay pmsCommentReplay = pmsCommentReplayService.getById(id);
		return Result.ok(pmsCommentReplay);
	}

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "商品评价回复-通过评论ID查询回复列表")
	 @ApiOperation(value="商品评价回复-通过评论ID查询回复列表", notes="商品评价回复-通过评论ID查询回复列表")
	 @GetMapping(value = "/queryListByCommentId")
	 public Result<?> queryListByCommentId(@RequestParam(name="id",required=true) Long id) {
		 Result<List<PmsCommentReplay>> result = new Result<>();
		 LambdaQueryWrapper<PmsCommentReplay> query = new LambdaQueryWrapper<>();
		 query.eq(PmsCommentReplay::getCommentId,id);
		 query.orderByDesc(PmsCommentReplay::getCreateTime);
		 List<PmsCommentReplay> list = pmsCommentReplayService.list(query);
		 result.setResult(list);
		 result.setSuccess(true);
		 return result;
	 }


  /**
   * 导出excel
   *
   * @param request
   * @param pmsCommentReplay
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, PmsCommentReplay pmsCommentReplay) {
      return super.exportXls(request, pmsCommentReplay, PmsCommentReplay.class, "商品评价回复");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, PmsCommentReplay.class);
  }

}
